﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace HenIT.Data.SqlClient
{
    public class EventScavengerDAL : GenericSQLServerDAL
    {
        private SqlConnection conn;

        public string InsertEventLogEntries(System.IO.Stream xml)
        {
            string infoResult = "";
            #region Connection reset
            if (conn == null || conn.State != ConnectionState.Open)
            {
                conn = null;
                conn = new SqlConnection(connectionString);
                conn.Open();
            }
            #endregion

            string sql = "InsertEventLogEntries";
            System.Data.SqlTypes.SqlXml sxl = new System.Data.SqlTypes.SqlXml(xml);
            SqlParameter[] parms = new SqlParameter[] {
                    new SqlParameter("@xml", SqlDbType.Xml) { Value = sxl}
                };
            using (SqlCommand cmnd = new SqlCommand(sql, conn))
            {
                cmnd.CommandType = CommandType.StoredProcedure;
                cmnd.CommandTimeout = CommandTimeout;
                cmnd.Parameters.AddRange(parms);
                try
                {
                    using (SqlDataReader r = cmnd.ExecuteReader())
                    {
                        if (r.Read())
                        {
                            int duplicates = (int)r["Duplicates"];
                            int repeats = (int)r["Repeats"];
                            if (duplicates > 1 || repeats > 0)
                            {
                                infoResult = string.Format("{0} inserts, {1} duplicates, {2} repeats, {3} failures, " +
                            "{4}ms duration, last err: {5}",
                                r["Inserts"],
                                r["Duplicates"],
                                r["Repeats"],
                                r["InsertFailures"],
                                r["MSDuration"],
                                r["LastErrorMsg"]);
                            }
                        }
                    }
                    DoRaiseDBAccessInsertsEvent();
                }
                catch
                {
                    DoRaiseDBAccessErrorsEvent();
                    throw;
                }
                finally
                {

                }
            }
            return infoResult;
        }
        public void SetLastMachineLogPollingTime(string machine, string logName, DateTime lastPollTime)
        {
            string sql = "SetLastPollTimeForMachineLog";
            #region Connection reset
            if (conn == null || conn.State != ConnectionState.Open)
            {
                conn = null;
                conn = new SqlConnection(connectionString);
                conn.Open();
            }
            #endregion
            try
            {
                using (SqlCommand cmnd = new SqlCommand(sql, conn))
                {
                    cmnd.CommandType = CommandType.StoredProcedure;
                    cmnd.CommandTimeout = CommandTimeout;
                    SqlParameter[] parms = new SqlParameter[] {
                    new SqlParameter("@Machine", machine),
                    new SqlParameter("@LogName", logName),
                    new SqlParameter("@LastPollTime", lastPollTime.AddSeconds(-3)) //Allow grace period of 3 seconds to avoid events being missed
                    };
                    cmnd.Parameters.AddRange(parms);
                    cmnd.ExecuteNonQuery();
                }
            }
            catch { throw; }
        }

        public void CloseConnection()
        {
            try
            {
                if (conn != null && conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                    conn = null;
                }
            }
            catch { }
        }
    }
}
